package com.infomanage.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.infomanage.exception.DaoException;
import com.infomanage.po.HibernateSessionFactory;
import com.infomanage.po.PersonFamilyInfo;

public class PersonFamilyInfoDAO extends BaseDAO<PersonFamilyInfo>{
	
	public PersonFamilyInfo findByPersonId(int personId) throws DaoException{
		Session session = HibernateSessionFactory.getSession();
		List<PersonFamilyInfo> list = null;
		try {
			Query query =  session.createQuery("from "+PersonFamilyInfo.class.getSimpleName()+" where personId = ?");
			query.setInteger(0, personId);
			list = query.list();
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return (list == null || list.size()==0) ? null : list.get(0);
	}
	
	public void removeByPersonId(int personId) throws DaoException{
		PersonFamilyInfo info = findByPersonId(personId);
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.delete(info);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	public void removeByPersonFamilyInfo(PersonFamilyInfo info) throws DaoException{
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.delete(info);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
		
	}
}
